//
//  MYAgentCreated.m
//  投融保
//
//  Created by 523 on 14-1-12.
//  Copyright (c) 2014年 木叶科技. All rights reserved.
//

#import "MYAgentCreated.h"
#import "MYCustomTextField.h"

@interface MYAgentCreated ()

@end

@implementation MYAgentCreated

extern NSString *key;
extern NSDictionary *userInformation ;//用户信息

@synthesize agmentServic,arrText,bgScrollView,btnProtocol,lblTitle;
@synthesize viewTransferredscceuss;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    SETBACKGROUND
    [self initView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView
{
    Y_=0;
    if (IOS_VERSION>=7.0) {
        Y_=20;
    }
    
    //bgScrollView手势
    UITapGestureRecognizer *gesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewEndEditing)];
    
    self.bgScrollView=[UIScrollView new];
    [self.bgScrollView setFrame:CGRectMake(0, Y_, SCREEN_WIDTH,SCREEN_HEIGHT-Y_)];
    int scroll_y=150;
    if (SCREEN_HEIGHT>480) {
        scroll_y=50;
    }
    [self.bgScrollView setContentSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT-Y_+scroll_y)];
    [self.bgScrollView setScrollEnabled:YES];
    [self.bgScrollView addGestureRecognizer:gesture];
    
    [self.view addSubview:bgScrollView];
    
    UIView *titleView=[[UIView alloc] initWithFrame:CGRectMake(0, Y_, 768, 104)];
    [titleView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"titile_bg"]]];
    [self.view addSubview:titleView];
    
    lblTitle=[[UILabel alloc] initWithFrame:CGRectMake(280, 0, 200, 104)];
    [lblTitle setText:@"投资人开户"];
    [lblTitle setTextColor:[UIColor whiteColor]];
    [lblTitle setFont:[UIFont boldSystemFontOfSize:40]];
    lblTitle.textAlignment=NSTextAlignmentCenter;
    lblTitle.backgroundColor=[UIColor clearColor];
    [titleView addSubview:lblTitle];
    
    
    UIButton *btnReturn=[UIButton new];
    [btnReturn setFrame:CGRectMake(0, 0, 105, 105)];
    [btnReturn setImage:[UIImage imageNamed:@"return_icon"] forState:UIControlStateNormal];
    [btnReturn.titleLabel setFont:[UIFont systemFontOfSize:22]];
    [btnReturn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnReturn addTarget:self action:@selector(btnReturnClick) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview: btnReturn];
    
    
    float point_x=34;
    float point_y=140;
    float width=700;
    float height=80;
    
    self.arrText=[NSMutableArray new];
    NSArray *arrPlaceholder=[[NSArray alloc] initWithObjects:@"请输入账户名",@"真实姓名", @"电子邮件",nil];
    for (int i=0; i<=2; i++) {
        MYCustomTextField *text=[[MYCustomTextField alloc] initWithFrame:CGRectMake(point_x, point_y, width, height)];
        [text setPlaceholder:[arrPlaceholder objectAtIndex:i]];
        [text setAutocorrectionType:UITextAutocorrectionTypeNo];
        [text setFont:[UIFont systemFontOfSize:28]];
        text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [text setBackground:[UIImage imageNamed:@"textfild_bg"]];
        text.tag=10060+i;
        text.delegate=self;
        [self.bgScrollView addSubview:text];
        [self.arrText addObject:text];
        point_y+=(height+20);
    }
    
    
    btnProtocol=[UIButton new];
    btnProtocol.frame=CGRectMake(point_x+20, point_y+4, 30, 30);
    btnProtocol.selected=YES;
    [btnProtocol setImage:[UIImage imageNamed:@"agreed_icon"] forState:UIControlStateSelected];
    [btnProtocol addTarget:self action:@selector(btnProtocolClick:) forControlEvents:UIControlEventTouchDown];
    [self.bgScrollView addSubview:btnProtocol];
    
    UILabel *lblProtocol=[UILabel new];
    [lblProtocol setFrame:CGRectMake(point_x+55, point_y-2, 400, 40)];
    lblProtocol.text=@"我已阅读并同意《用户协议》";
    lblProtocol.backgroundColor=[UIColor clearColor];
    lblProtocol.textColor=[UIColor darkGrayColor];
    lblProtocol.font=[UIFont systemFontOfSize:28];
    [self.bgScrollView addSubview:lblProtocol];
    point_y+=60;
    
    UIButton *btnRegister=[UIButton new];
    btnRegister.frame=CGRectMake(point_x, point_y, width, height);
    [btnRegister setBackgroundImage:[UIImage imageNamed:@"login_bg"] forState:UIControlStateNormal];
    [btnRegister setTitle:@"立即开户" forState:UIControlStateNormal];
    [btnRegister.titleLabel setFont:[UIFont systemFontOfSize:35]];
    [btnRegister addTarget:self action:@selector(btnNextStepClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgScrollView addSubview:btnRegister];
    
    
}

- (void)scrollViewEndEditing
{
    [self.bgScrollView endEditing:YES];
}


//返回前页按钮方法
- (void)btnReturnClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//协议选择
- (IBAction)btnProtocolClick:(UIButton *)sender
{
    if (sender.selected) {
        [sender setImage:[UIImage imageNamed:@"disagreed_icon"] forState:UIControlStateNormal];
        sender.selected=!sender.selected;
    }
    else
    {
        [sender setImage:[UIImage imageNamed:@"agreed_icon"] forState:UIControlStateSelected];
        sender.selected=!sender.selected;
    }
}

- (void)btnNextStepClick
{
    if (btnProtocol.selected) {   //判断是否选择同意协议
        
        //得到页面每个text 中的数据
        NSMutableArray *arrStr = [NSMutableArray new];
        for(int i = 0; i <= 2; i++)
        {
            NSString *strText=((UITextField *)[self.view viewWithTag:10060+i]).text;
            [arrStr addObject:strText];
        }
        
        
        //        NSString *strRegister = @"service=register&userName=jingjing&realName=晶晶&mail=masakap@gmail.com&certNo=50023319881029031&gender=1";
        //拼接待加密字符串
        NSString *strRegister=[NSString stringWithFormat:@"service=register&userName=%@&realName=%@&mail=%@&referees=%@",arrStr[0],arrStr[1],arrStr[2],[userInformation valueForKey:@"memberNo"]];
        
        self.agmentServic=[MYWebService new];
        NSDictionary *dict= [self.agmentServic getDataSynWithKey:key encryptedString:strRegister appKey:nil];
        [self createAccountResultWithCode:dict];

        
        
    }
}



#pragma mark +++++++++键盘+++++++++++


/**
 结束编辑UITextField的方法，让原来的界面还原高度
 */
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    
    //正则表达式验证
    switch (textField.tag) {
        case 10060://账户名
        {
            NSRegularExpression *regular=[NSRegularExpression regularExpressionWithPattern:@"[^A-Za-z0-9_]" options:0 error:nil];
            NSTextCheckingResult *result = [regular firstMatchInString:textField.text options:0 range:NSMakeRange(0, textField.text.length)];
            if (result) {
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:@"用户名为字母数字下划线组合" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
                [alert show];
                return;
            }
            
            NSString *strAppValidationUserNameService=[NSString stringWithFormat:@"service=appValidationUserNameService&userName=%@",textField.text];
            NSDictionary *dict =[[MYWebService new] getDataSynWithKey:key encryptedString:strAppValidationUserNameService appKey:nil];
            NSLog(@"%@",[dict valueForKey:@"code"]);
            if ([[dict valueForKey:@"code"] intValue] ==0&&textField.text.length>0) {
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:@"用户名已存在" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
                [alert show];
                return;
            }
            
        }
            break;
        case 10061://真实姓名
        {
            NSRegularExpression *regular=[NSRegularExpression regularExpressionWithPattern:@"[^x00-xff]" options:0 error:nil];
            NSTextCheckingResult *result = [regular firstMatchInString:textField.text options:0 range:NSMakeRange(0, textField.text.length)];
            if (!result) {
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:@"请输入真实姓名" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
                [alert show];
            }
        }
            break;
        case 10062://邮件
        {
            NSRegularExpression *regular=[NSRegularExpression regularExpressionWithPattern:@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" options:0 error:nil];
            NSTextCheckingResult *result = [regular firstMatchInString:textField.text options:0 range:NSMakeRange(0, textField.text.length)];
            if (!result) {
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:@"邮箱输入有误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
                [alert show];
            }
        }
            break;
        
        default:
            break;
    }
    
    
    
//    if(prewTag == -1) //当编辑的View不是需要移动的View
//    {
//        self.bgScrollView.scrollEnabled=NO;
//        return;
//    }
//    float moveY ;
//    NSTimeInterval animationDuration = 0.250f;
//    CGRect frame = self.bgScrollView.frame;
//    if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
//    {   //还原界面
//        moveY =  prewMoveY;
//        frame.origin.y +=moveY;
//        frame.size. height -=moveY;
//        [self.bgScrollView setContentOffset:CGPointMake(0, frame.origin.y-100)];
//    }
//    //bgScrollView移回原位置
//    [UIView beginAnimations:@"ResizeView" context:nil];
//    [UIView setAnimationDuration:animationDuration];
//    [self.bgScrollView setContentOffset:CGPointMake(0, frame.origin.y-100)];
//    [UIView commitAnimations];
//    [textField resignFirstResponder];
//    
    self.bgScrollView.scrollEnabled=NO;
    
}


//注册结果view
- (void)createAccountResultWithCode:(NSDictionary *)dict
{
    [self.bgScrollView setAlpha:0];
    NSString *str1,*str2,*str3,*str4;
    if ([[dict valueForKey:@"code"] intValue]==1) {
        str1=@"开户成功";
        str2=@"继续开户";
        str3=@"请提醒客户及时激活";
        str4=@"happyface";  //使用图片名称
    }else{
        str1=@"开户失败";
        str2=@"重新开户";
        str3=[dict valueForKey:@"message"];
        str4=@"Failed";
    }
    self.lblTitle.text=str1;
    Y_=0;
    if (IOS_VERSION>=7) {
        Y_=20;
    }
    viewTransferredscceuss=[[UIView alloc] initWithFrame:CGRectMake(0, 88+Y_, SCREEN_WIDTH, SCREEN_HEIGHT-88+Y_)];
    [viewTransferredscceuss setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:viewTransferredscceuss];
    
    
    UIImageView *imageFace=[[UIImageView alloc] initWithFrame:CGRectMake(120, 120, 60, 60)];
    imageFace.image=[UIImage imageNamed:str4];
    [viewTransferredscceuss addSubview:imageFace];
    
    UILabel *lblResult=[[UILabel alloc] initWithFrame:CGRectMake(240, 120, 300, 60)];
    lblResult.text=str3;
    lblResult.textColor=UIColorFromRGB(0xb70100);
    lblResult.textAlignment=NSTextAlignmentCenter;
    lblResult.font=[UIFont systemFontOfSize:28];
    lblResult.backgroundColor=[UIColor clearColor];
    [viewTransferredscceuss addSubview:lblResult];
    
    //重新开户
    UIButton *btnReTrans=[[UIButton alloc] initWithFrame:CGRectMake(90, 280, 280, 88)];
    [btnReTrans setBackgroundImage:[UIImage imageNamed:@"login_bg"] forState:UIControlStateNormal];
    [btnReTrans setTitle:str2 forState:UIControlStateNormal];
    [btnReTrans setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnReTrans.titleLabel setFont:[UIFont systemFontOfSize:28]];
    [btnReTrans addTarget:self action:@selector(btnReCreate) forControlEvents:UIControlEventTouchUpInside];
    [viewTransferredscceuss addSubview:btnReTrans];
    //返回
    UIButton *btnReturnHomepage=[[UIButton alloc] initWithFrame:CGRectMake(388, 280, 280, 88)];
    [btnReturnHomepage setBackgroundImage:[UIImage imageNamed:@"register_bg"] forState:UIControlStateNormal];
    [btnReturnHomepage setTitle:@"返回" forState:UIControlStateNormal];
    [btnReturnHomepage setTitleColor:UIColorFromRGB(0xb70100) forState:UIControlStateNormal];
    [btnReturnHomepage.titleLabel setFont:[UIFont systemFontOfSize:28]];
    [btnReturnHomepage addTarget:self action:@selector(btnReturnClick) forControlEvents:UIControlEventTouchUpInside];
    [viewTransferredscceuss addSubview:btnReturnHomepage];
}
//重新开户
- (void)btnReCreate
{
    viewTransferredscceuss.alpha=0;
    self.bgScrollView.alpha=1;
}

#pragma mark-   -----------状态栏底色------
- (UIStatusBarStyle)preferredStatusBarStyle
{
    [self.view setBackgroundColor:[UIColor blackColor]];
    return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden
{
    return NO;
}




@end
